package com.sx.iptv.controller;

import com.sx.iptv.service.IptvService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/sxnl")
@RequiredArgsConstructor
public class SxnlTvController {
    private static final Logger logger = LoggerFactory.getLogger(SxnlTvController.class);

    private final IptvService iptvService;

    @GetMapping("/playUrl")
    public void getSxnlTv(HttpServletResponse response) {
        String url = iptvService.getPlayUrl();
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            if (StringUtils.hasLength(url)) {
                response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); // 设置状态码为302
                response.setHeader("Location", url); // 设置Location头部
                response.sendRedirect(url); // 发送重定向响应（这一步实际上是可选的，因为设置状态码和头部已经足够）
            }
            stopWatch.stop();
            logger.info("耗时:{}", stopWatch.getTotalTimeSeconds());
        } catch (Exception e) {
            logger.info("程序异常");
            logger.error(e.getLocalizedMessage(), e);
        } finally {
            logger.info("完毕!");
        }
    }
}
